package logic

import (
	"context"

	"gb28181/gb28181"
	"gb28181/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type QueryDeviceLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryDeviceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryDeviceLogic {
	return &QueryDeviceLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *QueryDeviceLogic) QueryDevice(in *gb28181.DeviceQueryRequest) (*gb28181.DeviceQueryResponse, error) {
	// 查询设备信息
	device, exists := l.svcCtx.ClientMgr.GetDevice(in.DeviceId)
	if !exists {
		return &gb28181.DeviceQueryResponse{
			Success: false,
			// Message:  "设备不存在",
			DeviceId: in.DeviceId,
		}, nil
	}

	// 检查设备是否在线
	status := "离线"
	if device.IsOnline() {
		status = "在线"
	}

	return &gb28181.DeviceQueryResponse{
		Success: true,
		// Message:       "查询成功",
		DeviceId:      in.DeviceId,
		Status:        status,
		SipUri:        device.SipUri,
		LastHeartbeat: device.LastHeartbeat.Format("2006-01-02 15:04:05"),
	}, nil
}
